<?php
/**
 * Created by PhpStorm.
 * User: wzy12
 * Date: 2018/10/10
 * Time: 16:36
 */

namespace app\common\model;


use think\Model;

class Friendly extends Model
{
    public function resList($data){
        $list = $this -> where($data) -> field('id,name,value,key,inc_type') -> paginate(10);

        return $list;
    }

    public function operation($data){
        $action = false;

        if(isset($data['id'])){
            $action = true;
        }else{
            $res = $this -> where('url', trim($data['url'])) -> value('id');
            if($res){
                return echoArr(0, '该链接已存在');
            }
        }

        $result = $this -> validate(true) -> allowField(true) -> isUpdate($action) -> save($data);
        if(false === $result){
            return echoArr(0, $this -> getError());
        }else{
            return echoArr(1, '操作成功');
        }
    }

    /**
     * 更改排序和状态
     */
    public function editSort($data){
        $result = $this -> validate('Friendly.num') -> allowField(true) -> isUpdate(true) -> save($data);

        if(false === $result){
            return echoArr(0, $this -> getError());
        }else{
            return echoArr(1, '更新成功');
        }
    }

    public function del($data){
        $result = $this -> destroy($data);

        if(false === $result){
            return echoArr(0, '操作失败');
        }else{
            return echoArr(1, '操作成功');
        }
    }
}